﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace rkShared.BEncode
{
    public class BEncodedInt : IBElement
    {
        public long Value { get; set; }
        public int IntValue
        {
            get
            {
                return (int)this.Value;
            }
            set
            {
                this.Value = value;
            }
        }
        public BEncodedInt()
        {
        }
        public BEncodedInt(long value)
        {
            this.Value = value;
        }
        public static explicit operator BEncodedInt(int value)
        {
            return new BEncodedInt((int)value);
        }
        public static explicit operator BEncodedInt(long value)
        {
            return new BEncodedInt(value);
        }
        public static implicit operator int(BEncodedInt value)
        {
            return value.IntValue;
        }
        public static implicit operator long(BEncodedInt value)
        {
            return value.Value;
        }

        #region IBElement Members

        public void Encode(BinaryWriter writer)
        {
            writer.Write(Encoding.ASCII.GetBytes(string.Format("i{0}e", this.Value.ToString())));
        }
        public void Decode(byte[] binary)
        {
            string _value = Encoding.ASCII.GetString(binary);
            this.Value = long.Parse(_value);
        }
        #endregion
    }
}
